package com.sx.pipeline.ui.supply.approve.detail

import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.lxj.xpopup.XPopup
import com.sx.mvvmlibs.base.EasyAdapter
import com.sx.pipeline.BR
import com.sx.pipeline.R
import com.sx.pipeline.adapter.ReleaseImageAdapter
import com.sx.pipeline.app.base.CYDataBindingBaseActivity
import com.sx.pipeline.app.config.IntentKey
import com.sx.pipeline.app.config.ReleaseSupplyType
import com.sx.pipeline.app.config.SupplyApprovalType
import com.sx.pipeline.arouth.RouteManager
import com.sx.pipeline.arouth.RoutePath
import com.sx.pipeline.databinding.ActSupplyApprovalDetailBinding
import com.sx.pipeline.ui.supply.ReleaseSupplyVM
import com.sx.pipeline.ui.supply.approve.ApprovalSupplyVM


/**
 * @Author: YY
 * @Date: 2022/4/5
 * @Description: 供应商 供应详情
 */
@Route(path = RoutePath.Mine.SUPPLY_APPROVAL_DETAIL)
class SupplyApprovalDetailActivity :
    CYDataBindingBaseActivity<ActSupplyApprovalDetailBinding, ApprovalSupplyVM>(R.layout.act_supply_approval_detail),
    View.OnClickListener {
    @JvmField
    @Autowired(name = IntentKey.ID)
    var supplyId: Int? = 0

    private var mImageAdapter: ReleaseImageAdapter? = null//附件图片适配器

    override fun initData() {
        super.initData()
        mBinding.setVariable(BR.onClick, this)
        mBinding.titleBar.leftView.setOnClickListener {
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        //请求供应详情
        supplyId?.let { mViewModel.getSupplyOrderDetail(it) }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        observe(mViewModel.supplyDetailEvent){
            when (it?.status) {
                //待审核
                SupplyApprovalType.PENDING_REVIEW.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_pending_review)

                    mBinding.tvDelete.visibility=View.VISIBLE
                    mBinding.tvAction.visibility=View.VISIBLE
                }
                //已通过
                SupplyApprovalType.TO_BE_INQUIRED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_passed)

                    mBinding.tvDelete.visibility=View.GONE
                    mBinding.tvAction.visibility=View.GONE
                }
                //已驳回
                SupplyApprovalType.REJECTED.code -> {
                    mBinding.tvStatus.text=getString(R.string.txt_rejected)

                    mBinding.tvDelete.visibility=View.GONE
                    mBinding.tvAction.visibility=View.GONE
                }
            }
            //单位名称
            mBinding.sbUnitName.rightText=it?.unitName
            //标题
            mBinding.sbTitle.rightText=it?.title
            //供应描述
            mBinding.sbRequirementDetails.rightText=it?.supplyDescribe
            //供应服务区域
            mBinding.sbServiceArea.rightText=it?.addressDetail
            //开始时间
            mBinding.sbStartTime.rightText=it?.startTime
            //结束时间
            mBinding.sbEndTime.rightText=it?.endTime
            //结算方式
            mBinding.sbSettlementMethod.rightText=it?.paymentMethod
            //供应负责人
            mBinding.sbContact.rightText=it?.contact
            //负责人联系电话
            mBinding.sbContactNumber.rightText=it?.contactPhone
            //供应新建人
            mBinding.sbCreateby.rightText=it?.createBy
            //发布时间
            mBinding.sbCreateTime.rightText=it?.createTime
            //通过时间
            mBinding.sbAuditTime.rightText=it?.auditTime
            if (it?.payMoney?.toInt()!=0){
                //管币价格
                mBinding.tvMoney.text = it?.payMoney.toString()
                mBinding.tabPrice.visibility=View.VISIBLE
            }else{
                mBinding.tabPrice.visibility=View.GONE
            }
            //附件
            if (!TextUtils.isEmpty(it?.annexUrl)){
                it?.annexUrl?.split(",")?.let { it1 -> initDocutmentImg(it1) }
                mBinding.tabImg.visibility=View.VISIBLE
            }
        }

        observe(mViewModel.auditSuccessEvent){
            supplyId?.let { mViewModel.getSupplyOrderDetail(it) }
        }
    }

    //附件
    fun initDocutmentImg(data: List<String>) {
        mImageAdapter = ReleaseImageAdapter(this, data)
        mBinding.rvImg.layoutManager =
            GridLayoutManager(this, 3)
        mBinding.rvImg.adapter = mImageAdapter
        mImageAdapter?.selectMode = EasyAdapter.SelectMode.SINGLE_SELECT
    }


    override fun onClick(v: View?) {
        when(v?.id){
            //查看供应设备详情
            R.id.sb_device_name-> mViewModel.supplyDetailEvent.value?.equipmentId?.let {
                RouteManager.Mine.intentToDeviceDetail(
                    it,true,false
                )
            }
            //同意供应
            R.id.tv_action->{
                supplyId?.let { mViewModel.getSupplyAudit(it,"",1) }
            }
            //拒接供应
            R.id.tv_delete->{
                XPopup.Builder(this)
                    .autoOpenSoftInput(true)
                    .isDestroyOnDismiss(true) //对于只使用一次的弹窗，推荐设置这个
                    .asInputConfirm(
                        getString(R.string.txt_please_input_reject_reason),
                        null,
                        "",
                        getString(R.string.txt_please_input),
                        {
                            if (TextUtils.isEmpty(it)) {
                                ToastUtils.showShort("请输入拒绝原因")
                                return@asInputConfirm
                            }
                            supplyId?.let { it1 -> mViewModel.getSupplyAudit(it1,it,2) }
                        },
                        null,
                        R.layout.dialog_task_reject
                    ) //最后一个参数绑定已有布局
                    .show()
            }
        }
    }

}